package com.bdqn.zhang.dao.impl;

import com.bdqn.zhang.dao.MemberinfoDao;
import com.bdqn.zhang.dao.impl.dbutil.DButil;
import com.bdqn.zhang.pojo.Memberinfo;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class MemberinfoDaoimpl extends DButil implements MemberinfoDao {
    Connection conn = null;
    PreparedStatement ment = null;
    ResultSet resultSet = null;

    @Override
    public List<Memberinfo> getAll(int from, int pageSixe) {
        Connection conn = null;
        PreparedStatement ment = null;
        ResultSet resultSet = null;
        conn = getConnerction();
        List<Memberinfo> list = new ArrayList();
        String sql = "select * from Member_info limit ?,?";
        try {
            ment = conn.prepareStatement(sql);
            //给参数传值
            ment.setObject(1, from);
            ment.setObject(2, pageSixe);
            resultSet = ment.executeQuery();
            if (resultSet != null) {
                while (resultSet.next()) {
                    Memberinfo memberinfo = new Memberinfo();
                    memberinfo.setId(resultSet.getString(1));
                    memberinfo.setPassword(resultSet.getString(2));
                    memberinfo.setEmail(resultSet.getString(3));
                    memberinfo.setName(resultSet.getString(4));
                    memberinfo.setPhone(resultSet.getString(5));
                    memberinfo.setDates(resultSet.getString(6));
                    memberinfo.setAddress(resultSet.getString(7));
                    memberinfo.setSex(resultSet.getString(8));
                    memberinfo.setPhoto(resultSet.getString(9));
                    memberinfo.setZhuangtai(resultSet.getInt(10));
                    list.add(memberinfo);
                }
            }
            return list;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            close(conn, ment, resultSet);
        }
    }

    @Override
    public List<Memberinfo> xb() {
        Connection conn = null;
        PreparedStatement ment = null;
        ResultSet resultSet = null;
        conn = getConnerction();
        List<Memberinfo> list = new ArrayList();
        String sql = "SELECT sex, count(sex) as sexzong FROM Member_info group by sex";
        try {
            ment = conn.prepareStatement(sql);
            resultSet = ment.executeQuery();
            if (resultSet != null) {
                while (resultSet.next()) {
                    Memberinfo memberinfo = new Memberinfo();
                    memberinfo.setSex(resultSet.getString(1));
                    memberinfo.setZong(resultSet.getInt(2));
                    list.add(memberinfo);
                }
            }
            return list;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            close(conn, ment, resultSet);
        }
    }
    @Override
    public int getCount() {
        conn = getConnerction();
        String sql = "select count(1) from Member_info";
        try {
            ment = conn.prepareStatement(sql);
            resultSet = ment.executeQuery();
            if (resultSet != null) {
                if (resultSet.next()) {
                    return resultSet.getInt(1);
                }
            }
            return 0;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            close(conn, ment, resultSet);
        }
    }

    @Override
    public List<Memberinfo> dan(String name, int from, int pageSixe) {
        Connection conn = null;
        PreparedStatement ment = null;
        ResultSet resultSet = null;
        conn = getConnerction();
        List<Memberinfo> list = new ArrayList();
        String sql = "select * from Member_info where concat(id,name,phone,sex,email,dates,address) like ? limit ?,?";
        try {
            ment = conn.prepareStatement(sql);
            //给参数传值
            ment.setObject(1, "%" + name + "%");
            ment.setObject(2, from);
            ment.setObject(3, pageSixe);
            resultSet = ment.executeQuery();
            if (resultSet != null) {
                while (resultSet.next()) {
                    Memberinfo memberinfo = new Memberinfo();
                    memberinfo.setId(resultSet.getString(1));
                    memberinfo.setPassword(resultSet.getString(2));
                    memberinfo.setEmail(resultSet.getString(3));
                    memberinfo.setName(resultSet.getString(4));
                    memberinfo.setPhone(resultSet.getString(5));
                    memberinfo.setDates(resultSet.getString(6));
                    memberinfo.setAddress(resultSet.getString(7));
                    memberinfo.setSex(resultSet.getString(8));
                    memberinfo.setPhoto(resultSet.getString(9));
                    memberinfo.setZhuangtai(resultSet.getInt(10));

                    list.add(memberinfo);
                }
            }
            return list;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            close(conn, ment, resultSet);
        }
    }
    @Override
    public int del(String id) {
        String sql = "DELETE from Member_info where id in (?)";
        Object[] obj = {id};
        return executeUpdate(sql, obj);
    }
    @Override
    public Memberinfo getStudentByid(String id) {
        Connection conn = null;
        PreparedStatement ment = null;
        ResultSet resultSet = null;
        conn = getConnerction();
        Memberinfo memberinfo = new Memberinfo();
        String sql = "select * from Member_info where id=?";
        try {
            ment = conn.prepareStatement(sql);
            ment.setObject(1, id);
            resultSet = ment.executeQuery();
            if (resultSet != null) {
                if (resultSet.next()) {
                    memberinfo.setId(resultSet.getString(1));
                    memberinfo.setPassword(resultSet.getString(2));
                    memberinfo.setEmail(resultSet.getString(3));
                    memberinfo.setName(resultSet.getString(4));
                    memberinfo.setPhone(resultSet.getString(5));
                    memberinfo.setDates(resultSet.getString(6));
                    memberinfo.setAddress(resultSet.getString(7));
                    memberinfo.setSex(resultSet.getString(8));
                    return memberinfo;
                }
            }
            return null;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            close(conn, ment, resultSet);
        }
    }

    @Override
    public int mhdan(String name) {
        conn = getConnerction();
        String sql = "select count(1) from Member_info where concat(id,name,phone,sex,email) like ?";
        try {
            ment = conn.prepareStatement(sql);
            ment.setObject(1, "%" + name + "%");
            resultSet = ment.executeQuery();
            if (resultSet != null) {
                if (resultSet.next()) {
                    return resultSet.getInt(1);
                }
            }
            return 0;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            close(conn, ment, resultSet);
        }
    }

    @Override
    public int add(Memberinfo memberinfo) {
        String sql = "INSERT INTO Member_info(password,email,name,phone,dates,address,sex,photo,zhuangtai) VALUES(?,?,?,?,?,?,?,?,?)";
        Object[] obj = {memberinfo.getPassword(),memberinfo.getEmail(), memberinfo.getName(), memberinfo.getPhone(), memberinfo.getDates(), memberinfo.getAddress(), memberinfo.getSex(), memberinfo.getPhoto(), memberinfo.getZhuangtai()};
        return executeUpdate(sql, obj);
    }

    @Override
    public int checkNameUnique(String name) {
        Connection conn = null;
        PreparedStatement ment = null;
        ResultSet resultSet = null;
        conn = getConnerction();
        String sql = "select count(1) from Member_info where name=?";
        try {
            ment = conn.prepareStatement(sql);
            ment.setObject(1, name);
            resultSet = ment.executeQuery();
            if (resultSet != null) {
                if (resultSet.next()) {
                    return resultSet.getInt(1);
                }
            }
            return 0;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            close(conn, ment, resultSet);
        }
    }

    @Override
    public int emails(String name) {
        Connection conn = null;
        PreparedStatement ment = null;
        ResultSet resultSet = null;
        conn = getConnerction();
        String sql = "select count(1) from Member_info where email=?";
        try {
            ment = conn.prepareStatement(sql);
            ment.setObject(1, name);
            resultSet = ment.executeQuery();
            if (resultSet != null) {
                if (resultSet.next()) {
                    return resultSet.getInt(1);
                }
            }
            return 0;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            close(conn, ment, resultSet);
        }
    }

    @Override
    public int edits(Memberinfo memberinfo) {
        String sql = "UPDATE Member_info SET email=?,name=?,phone=?,dates=?,address=?,sex=?,photo=? ,zhuangtai=? WHERE id=?";
        Object[] obj = {memberinfo.getEmail(), memberinfo.getName(), memberinfo.getPhone(), memberinfo.getDates(), memberinfo.getAddress(), memberinfo.getSex(), memberinfo.getPhoto(), memberinfo.getZhuangtai(), memberinfo.getId()};
        return executeUpdate(sql, obj);
    }

    @Override
    public int gai(Memberinfo memberinfo) {
        String sql = "UPDATE Member_info SET zhuangtai=? WHERE id=?";
        Object[] obj = {memberinfo.getZhuangtai(), memberinfo.getId()};
        return executeUpdate(sql, obj);
    }

    @Override
    public List<Memberinfo> ztcx(int name, int from, int pageSixe) {
        Connection conn = null;
        PreparedStatement ment = null;
        ResultSet resultSet = null;
        conn = getConnerction();
        List<Memberinfo> list = new ArrayList();
        String sql = "select * from Member_info where zhuangtai=? limit ?,?";
        try {
            ment = conn.prepareStatement(sql);
            //给参数传值
            ment.setObject(1, name);
            ment.setObject(2, from);
            ment.setObject(3, pageSixe);
            resultSet = ment.executeQuery();
            if (resultSet != null) {
                while (resultSet.next()) {
                    Memberinfo memberinfo = new Memberinfo();
                    memberinfo.setId(resultSet.getString(1));
                    memberinfo.setPassword(resultSet.getString(2));
                    memberinfo.setEmail(resultSet.getString(3));
                    memberinfo.setName(resultSet.getString(4));
                    memberinfo.setPhone(resultSet.getString(5));
                    memberinfo.setDates(resultSet.getString(6));
                    memberinfo.setAddress(resultSet.getString(7));
                    memberinfo.setSex(resultSet.getString(8));
                    memberinfo.setPhoto(resultSet.getString(9));
                    memberinfo.setZhuangtai(resultSet.getInt(10));
                    list.add(memberinfo);
                }
            }
            return list;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            close(conn, ment, resultSet);
        }
    }

    @Override
    public int mhztcx(int name) {
        conn = getConnerction();
        String sql = "select count(1) from Member_info where zhuangtai=?";
        try {
            ment = conn.prepareStatement(sql);
            ment.setObject(1, name);
            resultSet = ment.executeQuery();
            if (resultSet != null) {
                if (resultSet.next()) {
                    return resultSet.getInt(1);
                }
            }
            return 0;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            close(conn, ment, resultSet);
        }
    }

    @Override
    public List<Memberinfo> yhmcx(String name, int from, int pageSixe) {
        Connection conn = null;
        PreparedStatement ment = null;
        ResultSet resultSet = null;
        conn = getConnerction();
        List<Memberinfo> list = new ArrayList();
        String sql = "select * from Member_info where name like ? limit ?,?";
        try {
            ment = conn.prepareStatement(sql);
            //给参数传值
            ment.setObject(1, "%" + name + "%");
            ment.setObject(2, from);
            ment.setObject(3, pageSixe);
            resultSet = ment.executeQuery();
            if (resultSet != null) {
                while (resultSet.next()) {
                    Memberinfo memberinfo = new Memberinfo();
                    memberinfo.setId(resultSet.getString(1));
                    memberinfo.setPassword(resultSet.getString(2));
                    memberinfo.setEmail(resultSet.getString(3));
                    memberinfo.setName(resultSet.getString(4));
                    memberinfo.setPhone(resultSet.getString(5));
                    memberinfo.setDates(resultSet.getString(6));
                    memberinfo.setAddress(resultSet.getString(7));
                    memberinfo.setSex(resultSet.getString(8));
                    memberinfo.setPhoto(resultSet.getString(9));
                    memberinfo.setZhuangtai(resultSet.getInt(10));
                    list.add(memberinfo);
                }
            }
            return list;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            close(conn, ment, resultSet);
        }
    }

    @Override
    public List<Memberinfo> sjhcx(String name, int from, int pageSixe) {
        Connection conn = null;
        PreparedStatement ment = null;
        ResultSet resultSet = null;
        conn = getConnerction();
        List<Memberinfo> list = new ArrayList();
        String sql = "select * from Member_info where phone like ? limit ?,?";
        try {
            ment = conn.prepareStatement(sql);
            //给参数传值
            ment.setObject(1, "%" + name + "%");
            ment.setObject(2, from);
            ment.setObject(3, pageSixe);
            resultSet = ment.executeQuery();
            if (resultSet != null) {
                while (resultSet.next()) {
                    Memberinfo memberinfo = new Memberinfo();
                    memberinfo.setId(resultSet.getString(1));
                    memberinfo.setPassword(resultSet.getString(2));
                    memberinfo.setEmail(resultSet.getString(3));
                    memberinfo.setName(resultSet.getString(4));
                    memberinfo.setPhone(resultSet.getString(5));
                    memberinfo.setDates(resultSet.getString(6));
                    memberinfo.setAddress(resultSet.getString(7));
                    memberinfo.setSex(resultSet.getString(8));
                    memberinfo.setPhoto(resultSet.getString(9));
                    memberinfo.setZhuangtai(resultSet.getInt(10));
                    list.add(memberinfo);
                }
            }
            return list;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            close(conn, ment, resultSet);
        }
    }

    @Override
    public List<Memberinfo> xbcx(String name, int from, int pageSixe) {
        Connection conn = null;
        PreparedStatement ment = null;
        ResultSet resultSet = null;
        conn = getConnerction();
        List<Memberinfo> list = new ArrayList();
        String sql = "select * from Member_info where sex=? limit ?,?";
        try {
            ment = conn.prepareStatement(sql);
            //给参数传值
            ment.setObject(1, name);
            ment.setObject(2, from);
            ment.setObject(3, pageSixe);
            resultSet = ment.executeQuery();
            if (resultSet != null) {
                while (resultSet.next()) {
                    Memberinfo memberinfo = new Memberinfo();
                    memberinfo.setId(resultSet.getString(1));
                    memberinfo.setPassword(resultSet.getString(2));
                    memberinfo.setEmail(resultSet.getString(3));
                    memberinfo.setName(resultSet.getString(4));
                    memberinfo.setPhone(resultSet.getString(5));
                    memberinfo.setDates(resultSet.getString(6));
                    memberinfo.setAddress(resultSet.getString(7));
                    memberinfo.setSex(resultSet.getString(8));
                    memberinfo.setPhoto(resultSet.getString(9));
                    memberinfo.setZhuangtai(resultSet.getInt(10));
                    list.add(memberinfo);
                }
            }
            return list;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            close(conn, ment, resultSet);
        }
    }

    @Override
    public List<Memberinfo> yxcx(String name, int from, int pageSixe) {
        Connection conn = null;
        PreparedStatement ment = null;
        ResultSet resultSet = null;
        conn = getConnerction();
        List<Memberinfo> list = new ArrayList();
        String sql = "select * from Member_info where email like ? limit ?,?";
        try {
            ment = conn.prepareStatement(sql);
            //给参数传值
            ment.setObject(1, "%" + name + "%");
            ment.setObject(2, from);
            ment.setObject(3, pageSixe);
            resultSet = ment.executeQuery();
            if (resultSet != null) {
                while (resultSet.next()) {
                    Memberinfo memberinfo = new Memberinfo();
                    memberinfo.setId(resultSet.getString(1));
                    memberinfo.setPassword(resultSet.getString(2));
                    memberinfo.setEmail(resultSet.getString(3));
                    memberinfo.setName(resultSet.getString(4));
                    memberinfo.setPhone(resultSet.getString(5));
                    memberinfo.setDates(resultSet.getString(6));
                    memberinfo.setAddress(resultSet.getString(7));
                    memberinfo.setSex(resultSet.getString(8));
                    memberinfo.setPhoto(resultSet.getString(9));
                    memberinfo.setZhuangtai(resultSet.getInt(10));
                    list.add(memberinfo);
                }
            }
            return list;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            close(conn, ment, resultSet);
        }
    }

}
